<?php
namespace app\demo_record\controller;

use cmf\controller\AdminBaseController;
use app\demo_record\model\CategoryModel;
use tree\Tree;

class CategoryAdminController extends AdminBaseController
{
    public function showList()
    {
        $model=new CategoryModel();
        $list=$model->db()->paginate(20);
        $page=$list->render();
        foreach($list as $index => $item){
            $item["create_time_str"]=date("Y-m-d H:i:s",$item["create_time"]);
            $item["update_time_str"]=date("Y-m-d H:i:s",$item["update_time"]);
        }
        $this->assign("list",$list);
        $this->assign("page",$page);
        return $this->fetch("list");
    }
    public function showTreeTable()
    {
        $model=new CategoryModel();
        $list = $model->db()->select()->toArray();
        foreach($list as &$item){
            $item["show_edit_url"]=url('showEditForm',['id'=>$item['id']]);
            $item["create_sub_url"]=url('showCreateSubForm',['id'=>$item['id']]);
            $item["delete_item_url"]=url('deleteItem',['id'=>$item['id']]);
        }

        $tree       = new Tree();
        $tree->icon = ['&nbsp;&nbsp;│', '&nbsp;&nbsp;├─', '&nbsp;&nbsp;└─'];
        $tree->nbsp = '&nbsp;&nbsp;';

        $tree->init($list);
        $header_content = <<<EOT
<tr>
    <th width="50">编号</th>
    <th></th>
    <th>名称</th>
    <th>标题</th>
    <th width="200">操作</th>
</tr>
EOT;
        $row_template = <<<'EOT'
<tr>
    <td>$id</td>
    <td>$spacer</td>
    <td>$name</td>
    <td>$title</td>
    <td>
        <a href='$show_edit_url'>编辑</a>
        <a href='$create_sub_url'>创建下级分类</a>
        <a class='js-ajax-delete' href='$delete_item_url'>删除</a>
    </td>
</tr>
EOT;
        $rows_content = $tree->getTree(0, $row_template);
        $this->assign("header_content",$header_content);
        $this->assign("rows_content",$rows_content);

        return $this->fetch("tree_table");
    }
    public function showCreateForm()
    {
        return $this->fetch("create_form");
    }
    public function showCreateSubForm()
    {
        $id=input("id","");
        if( $id==="" ){
            $this->error("分类编号不能为空");
        }
        $this->assign("id",$id);
        return $this->fetch("create_form");
    }
    public function showEditForm()
    {
        $id=input("id","");
        if( $id==="" ){
            $this->error("分类编号不能为空");
        }
        $item=CategoryModel::get($id);
        $this->assign("item",$item);
        $parent=CategoryModel::get($item["parent_id"]);
        $this->assign("parent",$parent);
        return $this->fetch("edit_form");
    }
    public function insertItem()
    {
        $data=input();
        if( !isset($data["parent_id"]) || $data["parent_id"]==="" ){
            $this->error("上级分类不能为空");
        }
        if( !isset($data["name"]) || $data["name"]==="" ){
            $this->error("分类名称不能为空");
        }
        if( !isset($data["title"]) || $data["title"]==="" ){
            $data["title"]=$data["name"];
        }

        $model=new CategoryModel();
        $id=$model->db()->insertGetId($data);
        if(!$id){
            $this->error($model->getError());
        }
        $this->success("插入成功，id=$id");
    }
    public function updateItem()
    {
        $data=input();
        if( !isset($data["id"]) || $data["id"]==="" ){
            $this->error("分类编号不能为空");
        }
        else{
            $id=$data["id"];
            unset($data["id"]);
        }
        if( !isset($data["parent_id"]) || $data["parent_id"]==="" ){
            $this->error("上级分类不能为空");
        }
        if( !isset($data["name"]) || $data["name"]==="" ){
            $this->error("分类名称不能为空");
        }
        if( !isset($data["title"]) || $data["title"]==="" ){
            $data["title"]=$data["name"];
        }

        $model=CategoryModel::update($data,["id"=>$id]);
        if($model===false){
            $this->error("更新失败");
        }
        $this->success("更新成功");
    }
    public function deleteItem()
    {
        $id=input("id","");
        if( $id==="" ){
            $this->error("分类编号不能为空");
        }
        $model=new CategoryModel();
        $res=$model->db()->where(["id"=>$id])->delete();
        if($res===false){
            $this->error("删除失败");
        }
        $this->success("操作成功");
    }
    public function showTreeTableSelect()
    {
        $ids=input("ids","");
        if($ids !== ""){
            $ids=explode(",",$ids);
        }
        else{
            $ids=[];
        }
        $model=new CategoryModel();
        $list = $model->db()->select()->toArray();
        foreach($list as &$item){
            $item["checked"] = in_array($item["id"],$ids) ? "checked" : "";
        }

        $tree       = new Tree();
        $tree->icon = ['&nbsp;&nbsp;│', '&nbsp;&nbsp;├─', '&nbsp;&nbsp;└─'];
        $tree->nbsp = '&nbsp;&nbsp;';

        $tree->init($list);
        $header_content = <<<EOT
<tr>
    <th width="16">
        <label>
            <input type="checkbox" class="js-check-all" data-direction="x" data-checklist="js-check-x">
        </label>
    </th>
    <th width="50">编号</th>
    <th></th>
    <th>名称</th>
    <th>标题</th>
    <th width="160">操作</th>
</tr>
EOT;
        $row_template = <<<'EOT'
<tr class='data-item-tr'>
    <td>
        <input type='checkbox' class='js-check' data-yid='js-check-y' data-xid='js-check-x' name='ids[]' value='$id' data-name='$name' $checked>
    </td>
    <td>$id</td>
    <td>$spacer</td>
    <td>$name</td>
    <td>$title</td>
    <td>
    </td>
</tr>
EOT;
        $rows_content = $tree->getTree(0, $row_template);
        $this->assign("header_content",$header_content);
        $this->assign("rows_content",$rows_content);

        return $this->fetch("tree_table_select");
    }
}
